<?php
require_once(FRAMEWORK_DIR . 'JankController.php');
require_once(MODEL_DIR) . '/User/UserFactory.php';
require_once(MODEL_DIR) . '/Content/ContentFactory.php';
require_once('CMSParser.php');

/**
 * AdminController is an implementation of JankController for the JankCMS
 * application. It connects to the User and Content models, allowing it
 * to display information about the jankCMS content and edit it.
 *
 * @author Rick Hutcheson
 * @version 0.1
 */
class AdminController extends JankController {
	//TODO: Check necessity
	public $templateDir;
	private $userModel;
	private $contentModel;

	/**
	 * Creates a new AdminController, initialized with a set of parameters.
	 *
	 * @param array $parameters
	 * 	An array of the parameters passed to the application by the url.
	 */
	public function __construct($parameters) {

		// authentication
		session_start();
		$this->templateDir = APP_DIR . 'admin/pages';
		$this->userModel = new UserFactory();
		$this->contentModel = new ContentFactory();
		//TODO: We need to change the session stuff.
		$loggedIn = isset($_SESSION['username']);

		if ($loggedIn) {
			$user = $this->userModel->getUserFromUsername($_SESSION['username']);
			$this->values['user_name'] = $user->getFirstName() . " " .
			$user->getLastName();
		} else if (!empty($parameters) && $parameters[0] == 'login'){

		} else {
			$this->values['error_message'] = "You must login.";
			$parameters[0] = 'login';
		}
		parent::__construct($parameters);
	}

	/**
	 * This is the function which will run if no other is specified, or if
	 * the specified function is erroneous.
	 */
	public function run() {
		$this->display("dashboard");
	}

	/**
	 * Called if the user has not yet authenticated.
	 */
	public function login() {
		if (!empty($_POST['username']) && !empty($_POST['password'])) {
			try {
				if($this->userModel->attemptLogin($_POST['username'],
				$_POST['password'])) {
					// in this case, the user logged in successfully.
					$_SESSION['username'] = $_POST['username'];
					header('Location: ' . APP_DIR_LINKS . '/admin');
				} else {
					$this->values['error_message'] = "Incorrect Password.";
				}
			} catch(NoSuchUserException $ue) {
				$this->values['error_message'] = "Username does not exist.";
			}
		} else {
			$this->values['error_message'] =
                                    "Please login.";
		}
		$this->display("login");
	}

	//TODO: Work on this section.
	/**
	 * Called if the user selects the "content" tab. From this function, we
	 * can specify whether or not to create or manage content.
	 */
	public function content() {
		//rid ourselves of the "content" element from parameters
		if (!empty(	$this->parameters) &&
		method_exists($this, $this->parameters[0]. 'Content')) {
			$function = $this->parameters[0] . 'Content';

			// remove the name of the function from our parameters to make it
			// simpler for the called method to work with the data.
			array_shift($this->parameters);
			$this->$function();
		} else {
			// They've simply clicked content, so we display the content page
			// alone.
			$this->display("content");
		}
	}

	/**
	 * TODO: WRITE ME.
	 *
	 */
	public function createContent() {
		if(isset($_POST['form'])) {
			// Validate the data.
		}
		$this->display("create_content");
	}

	public function getContentList($limit) {
		return $this->contentModel->getLatestContent($limit);
	}
	public function getContentTypes() {
		return $this->contentModel->getContentTypes();
	}


}
